package com.yimi.yinhepda.adapter.RecycleAdapter;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.yimi.yinhepda.R;
import com.pda.ym.base.CommonRecycleAdapter;
import com.pda.ym.base.CommonViewHolder;
import com.yimi.yinhepda.entity.sign_manager.JieJianBreakCardEntity;
import com.pda.ym.utils.DateHelper;

import java.util.Date;
import java.util.List;

/**
 * 网点交件打卡适配器
 *
 * @author 000432
 * @date 2018/6/1 0001
 */

public class JieJianAdapter extends CommonRecycleAdapter<JieJianBreakCardEntity> {

    private Context mContext;
    private CommonViewHolder.onItemCommonClickListener mCommonClickListener;
    private List<JieJianBreakCardEntity> mDataList;

    public JieJianAdapter(Context context, List<JieJianBreakCardEntity> dataList, CommonViewHolder.onItemCommonClickListener commonClickListener) {
        super(context, dataList, R.layout.adapter_jiejian_break_card_item);
        this.mContext = context;
        this.mCommonClickListener = commonClickListener;
        this.mDataList = dataList;
    }

    @Override
    public void bindData(CommonViewHolder holder, final JieJianBreakCardEntity data, int position) {
        final TextView tvDate = holder.getView(R.id.tv_date);
        String breakDate = data.getScheduleDate() == null ? "" : data.getScheduleDate();
        if (breakDate.equals("")) {
            breakDate = DateHelper.getDateFormate(new Date());
        }
        data.setScheduleDate(breakDate);
        tvDate.setText(breakDate);
        holder.setText(R.id.tv_shiftsOrder, data.getShiftsOrder() + "班");
        String breaDate = data.getBreakDate() == null ? "" : data.getBreakDate();
        String breakTime = data.getBreakTime() == null ? "" : data.getBreakTime();
        holder.setText(R.id.tv_break_card_time, breaDate + "\n" + breakTime);
        holder.setText(R.id.tv_prv_station, data.getSourceZoneName());

        holder.setCommonClickListener(mCommonClickListener);
        View view = holder.getView(R.id.ll_jiejian_adapter_item);
        if (data.isCheck()) {
            view.setBackgroundColor(mContext.getResources().getColor(R.color.yellow));
        } else if (!breakTime.equals("")) {
            view.setBackgroundColor(mContext.getResources().getColor(R.color.green));
        } else {
            view.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }

        tvDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (JieJianBreakCardEntity entity : mDataList) {
                    entity.setCheck(false);
                }
                data.setCheck(true);
                notifyDataSetChanged();

                String clockDate = DateHelper.getDateFormate(new Date());

                //获得选择日期的当前时间戳
                long selectTextTime = DateHelper.getDate(clockDate).getTime();

                TimePickerDialog timePickerDialog = new TimePickerDialog.Builder().setCallBack(new OnDateSetListener() {
                    @Override
                    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                        String time = DateHelper.getDateToString(millseconds, DateHelper.dateFormat);
                        if (time.length() > 9) {
                            time = time.substring(0, 10);
                        }
                        tvDate.setText(time);
                        data.setScheduleDate(time);
                    }
                })
                        .setCancelStringId("取消")
                        .setSureStringId("确定")
                        .setTitleStringId("选择")
                        .setYearText("年")
                        .setMonthText("月")
                        .setDayText("日")
                        .setCyclic(false)
                        //设置能获取的最小值getMills（）是获取时间毫秒值
                        .setMinMillseconds(DateHelper.getDifferenceTime(-2))
                        //设置能到达的最大值
                        .setMaxMillseconds(System.currentTimeMillis())
                        //设置当前时间
                        .setCurrentMillseconds(selectTextTime)
                        .setThemeColor(mContext.getResources().getColor(R.color.timepicker_dialog_bg))
                        .setType(Type.YEAR_MONTH_DAY)
                        .setWheelItemTextNormalColor(mContext.getResources().getColor(R.color.timepicker_dialog_bg))
                        .setWheelItemTextSelectorColor(mContext.getResources().getColor(R.color.timepicker_toolbar_bg))
                        .setWheelItemTextSize(12)
                        .build();
                timePickerDialog.show(((FragmentActivity) mContext).getSupportFragmentManager(), "YEAR_MONTH_DAY");
            }
        });
    }
}
